package com.gcb.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BuildTable2Pojo{

   
    public String getPro(String table){
    	StringBuffer sb = new StringBuffer();
    	ConnOracle con = new ConnOracle();
    	ResultSet rs = con.query("select * from " + table);
    	try {
			ResultSetMetaData rsmd = rs.getMetaData();
			String sql = "select t.comments from user_col_comments t where t.table_name='"+table.toUpperCase()+"'";
			rs = con.query(sql);
			int counts = rsmd.getColumnCount();
	    	for(int i = 1;i <= counts;i++){
	    		String name = rsmd.getColumnName(i);
	    		String type = this.getType(rsmd.getColumnTypeName(i), table, name);
	    		rs.next();
	    		String comment = rs.getString(1);
	    		if(comment == null){
	    			sb.append("private " + type + " " + name.toLowerCase() + ";");
	    		}else{
	    			sb.append("private " + type + " " + name.toLowerCase() + ";//" + comment);
	    		}
	    		sb.append("\n");
	    	}
	    	
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return sb.toString();
    }
    
    /**
     * 数据库中字段类型与实体BEAN类型直接的转换，可根据需要进行修改
     * 
     * @author cb
     * @date 2006-11-29 9:33:17
     * @param type
     * @param table
     * @param name
     * @return
     */
    private String getType(String type, String table, String name)
    {
        if ("VARCHAR2".equalsIgnoreCase(type) || "CHAR".equalsIgnoreCase(type))
        {
            type = "String";
        }
        else if ("NUMBER".equalsIgnoreCase(type))
        {
            type = "BigDecimal";
        }
        else if ("DATE".equalsIgnoreCase(type))
        {
            type = "Date";
        }
        else if ("LONG".equalsIgnoreCase(type))
        {
            type = "Long";
        }
        else
        {
            System.out.println(table + "表出现未能处理的字段" + name + "类型为" + type);
        }
        return type;
    }
    
   
}
